package com.atguigu.controller;

import com.atguigu.client.CartFeignClient;
import com.atguigu.client.ProductFeignClient;
import com.atguigu.entity.SkuInfo;
import com.atguigu.util.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Controller
public class WebCartController {

    @Autowired
    private CartFeignClient cartFeignClient;
    @Autowired
    private ProductFeignClient productFeignClient;


    //1.加入购物车页面 http://cart.gmall.com/addCart.html?skuId=24&skuNum=1
    @RequestMapping("addCart.html")
    public String addCart(@RequestParam Long skuId, @RequestParam Long skuNum, HttpServletRequest request){
//        String userId = AuthContextHolder.getUserId(request);
//        String userTempId = AuthContextHolder.getUserTempId(request);
        cartFeignClient.addToCart(skuId,skuNum);
        //拿到商品的基本信息
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        request.setAttribute("skuInfo",skuInfo);
        request.setAttribute("skuNum",skuNum);
        return "cart/addCart";
    }

    //2.购物车列表
    @GetMapping("cart.html")
    public String cartIndex(){
        return "cart/index";
    }
}
